require "Net/send_Msg"
require "Net/TitleHandler"
require 'View/UIBase/UIBase'

UIBadge = UIBase.New(PanelNames.UIBadge);
local titleDecGroup = {};
local titleCount = 0;
local titleDecCount = 0;
local effect_mat = nil
local effect_mat2 = nil
local effect_mat3 = nil
local effect_mat4 = nil
local badgeListItemData = nil
local m_effect_matlist = nil;

local timerCountDown = {};
function UIBadge.Init(func)
    UIBase.Init(UIBadge, func);
end

--定义控件
function UIBadge:OnCreate()
    self:SetMedalData();
    local badgeList = self:GetChild("badgeList");
    self.titleDecList = badgeList:GetChild("list"):GetChild("list");
    self.titleDecBackBtn = badgeList:GetChild("back");
    self.bg = self:GetChild("btnBack");
    self.dragBounds = self:GetChild("BadgeCom");
    self.backBtn = self:GetChild("btnBack");
    self.lastBtn = self:GetChild("last");
    self.nextBtn = self:GetChild("next");
    self.nowPageText = self:GetChild("NowPage");
    self.badgeListController = self:GetController("BadgeList");
    self.pageController = self:GetController("page");
    self.pageController.selectedIndex = 0;
    self.attrController = self:GetController("attr");
    self.attrList = self:GetChild("attrList");
    self.maxPageText = self:GetChild("TotalPage");
    self.bgClose = self:GetChild("bgClose");
    self.maxPageText.text = self.maxPage;
    local pageBtnController = self:GetController("pageBtn");
    pageBtnController.selectedIndex = self.maxPage > 1 and 1 or 0;
    self:refreshChangeBtn();
    self.badges = {}
    self.badgeRect = {}
    self.badgePos = {}
    self.badgeIdx = {};
    self.titleList = self:GetChild("list/list");
    for i = 1, 5 do
        local tempobj = self.dragBounds:GetChild("Badge" .. i);
        tempobj.draggable = true;
        tempobj.onTouchBegin:Add(function()
            self:onDragStartFunc(tempobj)
        end);
        tempobj.onTouchMove:Add(function()
            self:onDragMoveFunc(tempobj)
        end);
        tempobj.onTouchEnd:Add(function()
            self:onDragEndFunc(tempobj)
        end);
        tempobj.onClick:Add(function()
            self:clickTitle(self.showTitles[i].id);
        end)
        --table.insert(self.badgePos, Vector2(tempobj.x, tempobj.y));
        table.insert(self.badges, tempobj);
        table.insert(self.badgeIdx, tempobj.parent:GetChildIndex(tempobj));

    end
    for i = 1, 5 do
        table.insert(self.badgeRect, self.dragBounds:GetChild("BadgeRect" .. i));
    end
    self:SetDynamicList(self.attrList, self.AttrItemRender)
    self:addEvent();
    self:RegisterEvent(Config.EventType.TitleInfo, self.setInfo);
    logicMgr.TitleManager.setIsNewTitle(0);
    --self.CloseFom();
end

function UIBadge:OnEnable()
    self:SetMedalData();
    TitleHandler.SendTitleInfo();
end

function UIBadge:OnDisable()
    for k, v in pairs(self.badges) do
        v.onClick:Clear();
    end

    FireEvent(Config.EventType.TitleInfo, saveList);
    TitleHandler.SendTitleInfo();
    logicMgr.TitleManager.SetShowList(saveList);
end

function UIBadge:OnDestroy()
    self:UnRegisterEvent(Config.EventType.TitleInfo, self.setInfo, self);
    for k, v in pairs(timerCountDown) do
        utimer.remove(v);
        v = nil;
    end
    if m_effect_matlist then
        for i = 0, m_effect_matlist.Length - 1 do
            m_effect_matlist:GetValue(i):SetFloat("_StencilComp", 8)
        end
        m_effect_matlist = nil
    end
    self:ClearMat()
end

function UIBadge:ClearMat()
    if effect_mat then
        effect_mat:SetFloat("_StencilComp", 8)
    end
    if effect_mat2 then
        effect_mat2:SetFloat("_StencilComp", 8)
    end
    if effect_mat3 then
        effect_mat3:SetFloat("_StencilComp", 8)
    end
    if effect_mat4 then
        effect_mat4:SetFloat("_StencilComp", 8)
    end
    effect_mat4 = nil
    effect_mat3 = nil
    effect_mat2 = nil
    effect_mat = nil
end

function UIBadge:addEvent()
    --btnChangeFormation.onClick:Add(function()
    --    self.ShowSelectFom(1);
    --end);
    self:RegisterOnClick(self.bgClose, self.onCloseClick);
    self:RegisterOnClick(self.backBtn, self.onCloseClick);
    self:RegisterOnClick(self.bg, self.onCloseClick);
    self:RegisterOnClick(self.lastBtn, self.OnLast);
    self:RegisterOnClick(self.nextBtn, self.OnNext);
    self:RegisterOnClick(self.titleDecBackBtn, self.titleDecBack);

end

function UIBadge:removeEvent()
    ----bt_SetBtn.onClick:Clear();
    --self.backBtn.onClick:Clear();
    --self.bg.onClick:Clear();
    --self.lastBtn.onClick:Clear();
    --self.nextBtn.onClick:Clear();
    --self.titleDecBackBtn.onClick:Clear();
end

function UIBadge:titleDecBack()
    self.badgeListController.selectedIndex = 0;
    curTitleDecs = {};
    self:ClearMat()
end

function UIBadge:OnNext()
    if self.pageController.selectedIndex < self.maxPage then
        self.pageController.selectedIndex = self.pageController.selectedIndex + 1;
        self:refreshChangeBtn();
    end
end

function UIBadge:OnLast()
    if self.pageController.selectedIndex > 0 then
        self.pageController.selectedIndex = self.pageController.selectedIndex - 1;
        self:refreshChangeBtn();
    end
end

function UIBadge:refreshChangeBtn()
    if self.pageController.selectedIndex == 0 then
        self.lastBtn.visible = false;
    else
        self.lastBtn.visible = true;
    end
    if self.pageController.selectedIndex == self.maxPage - 1 then
        self.nextBtn.visible = false;
    else
        self.nextBtn.visible = true;
    end
    self.nowPageText.text = self.pageController.selectedIndex + 1;
end

function UIBadge:onDragStartFunc(evt)
    local item = evt;
    if evt.draggable then
        evt.parent:SetChildIndex(evt, 999);
    end
end

function UIBadge:onDragMoveFunc(evt)
    local item = evt;
    if evt.draggable then
        local index = 0;
        for i = 1, #self.badges do
            if self.badges[i] == item then
                index = i;
                break ;
            end
        end
        local inPos = 0;
        for i = 1, #self.badgeRect do
            if isIntersect(self.badgeRect[i], item) == true then
                inPos = i;
                break
            end
        end

        for i = 1, #self.badgeRect do
            if (index ~= i) then
                self.badges[i]:SetPosition(self.badgeRect[i].x, self.badgeRect[i].y, 0);
            end
        end
    end
end

function UIBadge:onDragEndFunc(evt)
    local item = evt;
    if evt.draggable then
        local index = 0;
        for i = 1, #self.badges do
            if self.badges[i] == item then
                index = i;
                break ;
            end
        end
        local inPos = 0;
        for i = 1, #self.badgeRect do
            if isIntersect(self.badgeRect[i], item) == true then
                inPos = i;
                break
            end
        end
        if (inPos ~= 0) then
            self:changePosByIndex(index, inPos);
            --else
        end
        item:SetPosition(self.badgeRect[index].x, self.badgeRect[index].y, 0);
        if (index ~= 0) then
            item.parent:SetChildIndex(item, self.badgeIdx[index]);
        end
    end
end

function UIBadge:changePosByIndex(idx1, idx2)
    local temp = {};
    temp.id = self.showTitles[idx1].id;
    temp.info = self.showTitles[idx1].info;
    self.showTitles[idx1] = self.showTitles[idx2];
    self.showTitles[idx2] = temp;
    self:rendererShowTitle();
end

function UIBadge:onCloseClick()
    self:saveTitle();
    closeUI(PanelNames.UIBadge);
end

function UIBadge:rendererShowTitle()
    --if self.showTitles[1].id == 0 then
    --第一个icon没有
    local temp = {};
    for i = 1, #self.showTitles do
        if self.showTitles[i].id ~= 0 then
            table.insert(temp, self.showTitles[i]);
        end
    end
    for i = #temp + 1, 5 do
        local item = {};
        item.id = 0
        item.info = {};
        table.insert(temp, item);
    end
    self.showTitles = temp;
    --end

    for i = 1, #self.badges do
        local info = self.showTitles[i];
        local obj = self.badges[i];
        local icon = obj:GetChild("icon");

        if obj.dragBounds == nil or obj.dragBounds.y ~= self.dragBounds.y then
            obj.dragBounds = UnityEngine.Rect.New(self.dragBounds.x, self.dragBounds.y, self.dragBounds.actualWidth, self.dragBounds.actualHeight);
        end

        local stateController = obj:GetController("state");
        local effect = obj:GetChild("Effect_Dwsg");
        local effect2 = obj:GetChild("effect_huizsj");
        if info.id == 0 then
            stateController.selectedIndex = 0;
            obj.draggable = false;
            effect.visible = false
            effect2.visible = false
        else
            stateController.selectedIndex = 1;
            obj.draggable = i ~= 1;
            effect.visible = true
            --if tonumber(info.info.f_MedalPic) then
            --    icon.url = "ui://ItemAtlas/" .. info.info.f_MedalPic;
            --else
            --end
            icon.url = "ui://ICONAtlas/" .. info.info.f_MedalPic;
            if info.info.f_GroupId == 6 then
                --竞技场等级徽章加闪光特效，TODO有残留
                effect.visible = true;
                effect2.visible = false
                local level = info.info.f_LevelId;
                effect.animationName = "idle" .. level;
                effect.skinName = level;
                effect.loop = true;
            else
                effect2.visible = true
                effect.visible = false;
            end
        end
    end
end

function UIBadge:setInfo()
    local msg = logicMgr.TitleManager.GetTitleData();
    if msg == nil then
        return ;
    end
    self.allTitles = msg.titleList;
    for i = 1, #msg.showList do
        --if
        self.showTitles[i].id = msg.showList[i];
        self.showTitles[i].info = G.dataTable["t_medal"][tostring(msg.showList[i])];--表格消息
    end
    self:rendererShowTitle();
    self.itemTitleForId = {};
    self.titleList.itemRenderer = self.rendererTitles;
    titleCount = 0;
    self.titleList.numItems = #titleDecGroup;
    self:CalcAllAttr()
end

function UIBadge:getTitleInfoById(id)
    for i = 1, #self.allTitles do
        if self.allTitles[i].id == id then
            return self.allTitles[i];
        end
    end
    return nil;
end

function UIBadge:rendererTitles(item)
    titleCount = titleCount + 1;
    local idx = titleCount;
    local showId = 0
    local data = nil;
    for i = 1, #titleDecGroup[idx] do
        local titleInfo = UIBadge:getTitleInfoById(titleDecGroup[idx][i].f_Id);
        if UIBadge:getTitleInfoById(titleDecGroup[idx][i].f_Id) ~= nil and ((titleInfo.lastTime - HelperFunc.GetServerTime()) > 0 or titleInfo.lastTime == -1) then
            data = titleDecGroup[idx][i];
            break ;
        end
    end
    local name = item:GetChild("name");
    local ranklevel = item:GetChild("ranklevel");
    local icon = item:GetChild("icon");
    local decBtn = item:GetChild("btnDec");
    local txtTime = item:GetChild("time");
    local lockController = item:GetController("mask");
    local chooseController = item:GetController("choose");
    local timeLimitController = item:GetController("type");
    local effect = icon:GetChild("Effect_Dwsg");
    local effect2 = icon:GetChild("effect_huizsj");
    local effect3 = icon:GetChild("effect_huizsj_back");
    chooseController.selectedIndex = 0

    item.onTouchEnd:Clear()
    item.onTouchBegin:Clear()
    item.onTouchMove:Clear()

    if data == nil then
        data = titleDecGroup[idx][#titleDecGroup[idx]]
        lockController.selectedIndex = 1
        effect.visible = false;
        effect2.visible = false;
        effect3.visible = false;
        timeLimitController.selectedIndex = 0
    else
        lockController.selectedIndex = 0
        --item.onClick:Clear();
        --item.onClick:Add(function()
        --    UIBadge:clickTitle(data.f_Id, item);
        --end);
        item.onTouchEnd:Add(function()
            UIBadge:OnTouchUp(data, item)
        end)
        for i = 1, #UIBadge.showTitles do
            if UIBadge.showTitles[i].id == data.f_Id then
                chooseController.selectedIndex = 1;
                break ;
            end
        end
        if data.f_GroupId == 6 then
            --竞技场等级徽章加闪光特效
            effect.visible = true
            effect2.visible = false
            effect3.visible = false
            local level = data.f_LevelId;
            effect.animationName = "idle" .. level;
            effect.skinName = level;
            effect.loop = true;
        else
            effect2.visible = true
            effect3.visible = true
            effect.visible = false
        end
        local titleInfo = UIBadge:getTitleInfoById(data.f_Id);
        local countTime = titleInfo.lastTime - HelperFunc.GetServerTime()
        if countTime > 0 and titleInfo.lastTime ~= -1 then
            timeLimitController.selectedIndex = 1
            txtTime.text = HelperFunc.GetTimeStr2(countTime);
            timerCountDown[titleCount] = utimer.timeCall(function()
                countTime = countTime - 1;
                if countTime < 0 then
                    countTime = 0;
                end
                txtTime.text = HelperFunc.GetTimeStr2(countTime);
            end, 1000, -1);
        else
            timeLimitController.selectedIndex = 0
        end
    end
    UIBadge.itemTitleForId[data.f_Id] = item;
    --decBtn.onClick:Clear();
    --decBtn.onClick:Add(function()
    --    UIBadge:showTitleDec(data.f_GroupId);
    --end)

    item.onTouchBegin:Add(function(evt)
        UIBadge:OnTouchStart(evt, idx)
    end)
    item.onTouchMove:Add(function(evt)
        UIBadge:OnTouchMove(evt)
    end)

    icon:GetController("state").selectedIndex = 1;
    --if tonumber(data.f_MedalPic) then
    --    icon.icon = "ui://ItemAtlas/" .. data.f_MedalPic;
    --else
    --    icon.icon = "ui://ICONAtlas/" .. data.f_MedalPic;
    --end

    icon.icon = "ui://ICONAtlas/" .. data.f_MedalPic;

    name.text = getLanguage(data.f_MedalName);
    ranklevel.text = data.f_LevelId;
    if m_effect_matlist == nil then
        m_effect_matlist = Util.SetSkeletonAnimMatrialStencil(item, 3)
    end
end

function UIBadge:OnTouchStart(evt, heroData)
    self.lastEventX = evt.inputEvent.x;
    self.lastEventY = evt.inputEvent.y;
    self.isTouching = true;
    if self.timerId then
    else
        self.timerId = utimer.timeCall(function()
            if self.timerId ~= nil then
                if math.abs(self.lastEventY - evt.inputEvent.y) > 20 or math.abs(self.lastEventX - evt.inputEvent.x) > 20 then
                    --utimer.remove(self.timerId);
                    self.timerId = nil;
                    self.isTouching = false;
                    return ;
                end
            end
            self.isNotNeedEnable = true;
            UIBadge:showTitleDec(heroData);
            self.timerId = nil;
        end, 500, 1);
    end
end

--function UIBadge:OnTouchMove(evt)
--    if self.timerId ~= nil then
--        if math.abs(self.lastEventY - evt.inputEvent.y) > 5 or math.abs(self.lastEventX - evt.inputEvent.x) > 5 then
--            utimer.remove(self.timerId);
--            self.timerId = nil;
--            self.isTouching = false;
--        end
--    end
--end

function UIBadge:OnTouchUp(data, item)
    if self.timerId ~= nil and self.isTouching then
        utimer.remove(self.timerId);
        --上阵
        UIBadge:clickTitle(data.f_Id, item);
        self.timerId = nil;
    end
end

function UIBadge:clickTitle(id, obj)
    local beDelete = false;
    local beInsert = false;
    local beInsertNum = 0;
    for i = 1, #self.showTitles do
        if self.showTitles[i].id == id and id ~= 0 then
            if obj == nil then
                obj = self.itemTitleForId[id];
            end
            obj:GetController("choose").selectedIndex = 0;
            self.showTitles[i].id = 0;
            self.showTitles[i].info = {};
            beDelete = true;
            break ;
        end

    end
    if not beDelete then
        for i = 1, #self.showTitles do
            if self.showTitles[i].id == 0 then
                beInsertNum = i;
                beInsert = true;
                break ;
            end
        end
    else
        self:rendererShowTitle();
        return ;
    end

    if beInsert then
        if beInsertNum > 0 then
            self.showTitles[beInsertNum].id = id;
            self.showTitles[beInsertNum].info = G.dataTable["t_medal"][tostring(id)]
            if obj ~= nil then
                obj:GetController("choose").selectedIndex = 1;
            end
            self:rendererShowTitle();
        end

    else
        --展示勋章满了
    end
end

function UIBadge:showTitleDec(groupId)
    self.badgeListController.selectedIndex = 1;
    curTitleDecs = titleDecGroup[groupId];
    --self.titleDecList.itemRenderer = self.rendererTitlesDec;
    self:SetDynamicList(self.titleDecList, self.rendererTitlesDec)
    titleDecCount = 0;
    self.titleDecList.numItems = #curTitleDecs;

end

function UIBadge:rendererTitlesDec(index, item)
    titleDecCount = titleDecCount + 1;
    local idx = titleDecCount;

    local info = curTitleDecs[idx]
    badgeListItemData = info
    local lockController = item:GetController("lock");
    item:GetController("arrow").selectedIndex = idx - 1 == 0 and 1 or 0;
    local infoDecText = item:GetChild("info");
    local name = item:GetChild("name");
    local ranklevel = item:GetChild("ranklevel");
    local data = UIBadge:getTitleInfoById(info.f_Id);
    local icon = item:GetChild("icon");
    local effect2 = icon:GetChild("effect_huizsj");
    local effect = icon:GetChild("Effect_Dwsg");
    local attrList = item:GetChild("attrList");
    local attrData = decodeJsonStrToTabel(info.f_CharacterValue)
    self:SetDynamicList(attrList, self.BadgeListAttrItemRender)
    attrList.numItems = #attrData
    icon:GetController("state").selectedIndex = 1;
    -- icon.icon = "ui://ICONAtlas/" .. info.f_MedalPic;
    --if tonumber(info.f_MedalPic) then
    --    icon.icon = "ui://ItemAtlas/" .. info.f_MedalPic;
    --else
    --end
    icon.icon = "ui://ICONAtlas/" .. info.f_MedalPic;

    if effect_mat == nil then
        local effect = icon:GetChild("Effect_Dwsg")
        local effect2 = icon:GetChild("effect_huizsj")
        local t = effect.displayObject.cachedTransform:Find("GoWrapper/effect_dwsg_SkeletonData")
        if t then
            local anim = t:GetComponent("SkeletonAnimation")
            effect_mat = Util.GetMaterialOnSkeletonAnimation(anim, "effect_dwsg_Material")
            if effect_mat then
                effect_mat:SetFloat("_StencilComp", 3)
            end

            effect_mat2 = Util.GetMaterialOnSkeletonAnimation(anim, "effect_dwsg_Material-Additive")
            if effect_mat2 then
                effect_mat2:SetFloat("_StencilComp", 3)
            end
        end
        local t2 = effect2.displayObject.cachedTransform:Find("GoWrapper/effect_huizsj_SkeletonData")
        if t2 then
            local anim2 = t:GetComponent("SkeletonAnimation")
            effect_mat3 = Util.GetMaterialOnSkeletonAnimation(anim2, "effect_huizsj_Material-Additive")
            if effect_mat3 then
                effect_mat3:SetFloat("_StencilComp", 3)
            end
            effect_mat4 = Util.GetMaterialOnSkeletonAnimation(anim2, "effect_huizsj_Material")
            if effect_mat4 then
                effect_mat4:SetFloat("_StencilComp", 3)
            end
        end
    end

    if data == nil or ((data.lastTime - HelperFunc.GetServerTime()) <= 0 and data.lastTime ~= -1) then
        lockController.selectedIndex = 1;
        local locktips = item:GetChild("locktips");
        locktips.text = getLanguage(info.f_MedalUnlockDes);
        effect.visible = false;
        effect2.visible = false
    else
        local timeController = item:GetController("timelimit");
        local arrowController = item:GetController("arrow");
        arrowController.selectedIndex = idx - 1 == 0 and 1 or 0;
        local dateText = item:GetChild("date");
        local date = formatUnixTime2Date(data.bornTime);

        lockController.selectedIndex = 0;

        dateText.text = date.year .. "-" .. date.month .. "-" .. date.day;

        if data.lastTime <= -1 then
            timeController.selectedIndex = 0;
        else
            item:GetChild("timelimit").text = timeToTimeLeft(data.lastTime - HelperFunc.GetServerTime(), nil, true);
            timeController.selectedIndex = 1;
        end

        if info.f_GroupId == 6 then
            --竞技场等级徽章加闪光特效
            local effect = icon:GetChild("Effect_Dwsg");
            local level = info.f_LevelId;
            effect.animationName = "idle" .. level;
            effect.skinName = level;
            effect.loop = true;
            effect2.visible = false
        else
            effect.visible = false
            effect2.visible = true
        end
    end
    infoDecText.text = getLanguage(info.f_MedalDes);
    name.text = getLanguage(info.f_MedalName);
    ranklevel.text = info.f_LevelId;
end

function UIBadge:saveTitle()
    saveList = {}
    saveList.showList = {};
    for i = 1, #self.showTitles do
        local idNum = 0;
        if self.showTitles[i].id == 0 then
            table.insert(saveList.showList, nil);

        else
            table.insert(saveList.showList, self.showTitles[i].id);
        end
    end
    TitleHandler:SaveShowTitle(saveList.showList);
end

function UIBadge:CalcAllAttr()
    self.allAttr = logicMgr.TitleManager.GetAllAttr()  --已经激活的徽章
    self.sortAttrLst = {}
    if self.attributeShowConfig == nil then
        self.attributeShowConfig = G.dataTable["t_attributelibrary"];
    end
    local count = 0
    for k, v in pairs(self.allAttr) do
        count = count + 1
        local oneAttr = {}
        oneAttr.index = k
        oneAttr.value = v
        table.insert(self.sortAttrLst, oneAttr)
    end
    table.sort(self.sortAttrLst, function(a, b)
        local config_a = self.attributeShowConfig[tostring(a.index)];
        local config_b = self.attributeShowConfig[tostring(b.index)];
        return config_a.f_Sort < config_b.f_Sort
    end)
    self.attrList.numItems = count
    if count > 0 then
        self.attrController.selectedIndex = 1
    else
        self.attrController.selectedIndex = 0
    end
end

function UIBadge:AttrItemRender(index, go)
    local attr = self.sortAttrLst[index]
    local attributeItemConfig = self.attributeShowConfig[tostring(attr.index)];
    if attributeItemConfig.f_IsPanel == 1 then
        local ctrlItem = CtrlManager.GetCtrl(PanelNames.UICharactorAttributeDetailsItem);
        if ctrlItem ~= nil and go ~= nil then
            local data = {
                name = getLanguage(string.format(Config.LangAttribute, attr.index)),
                value = attr.value,
                type = attributeItemConfig.f_AttributeType,
                icon = "attr" .. attr.index
            };
            ctrlItem.initData(go, data);
        end
    end
end
function UIBadge:BadgeListAttrItemRender(index, go)
    local txtContent = go:GetChild("number")
    local curBadgeAttrData = decodeJsonStrToTabel(badgeListItemData.f_CharacterValue)
    if curBadgeAttrData then
        local name = getLanguage(string.format(Config.LangAttribute, curBadgeAttrData[index][1])) or " "
        txtContent.text = name .. " " .. logicMgr.EquipManager.formatAttr(curBadgeAttrData[index][1], curBadgeAttrData[index][2] or 0)
    end
end

function UIBadge:SetMedalData()
    titleDecGroup = {};
    self.titlesData = {};
    self.showTitles = {};
    self._curData = {};
    for k, v in pairs(G.dataTable["t_medal"]) do
        if self._curData[v.f_GroupId] == nil then
            table.insert(titleDecGroup, {});
            self._curData[v.f_GroupId] = #titleDecGroup;
        end
        table.insert(titleDecGroup[self._curData[v.f_GroupId]], v);
        table.sort(titleDecGroup[self._curData[v.f_GroupId]], function(a, b)
            return a.f_LevelId > b.f_LevelId
        end)
        table.insert(self.titlesData, v);
    end
    table.sort(titleDecGroup, function(a, b)
        return a[1].f_Id < b[1].f_Id
    end)
    self.maxPage = math.ceil(#titleDecGroup / 12);

    table.sort(self.titlesData, function(a, b)
        return a.f_Id < b.f_Id
    end)
    for i = 1, 5 do
        self.showTitles[i] = {};
        self.showTitles[i].id = 0;
        self.showTitles[i].info = {};
    end
end
return UIBadge;